package com.design.pages;
import com.design.config.GameFile;
import com.design.config.MoveBoxConfig;
import com.design.fill.FillBox;
import com.design.method.enums.moveBox.MapStaticEnum;
import com.design.method.error.ParserError;
import com.design.method.moveBox.MapStatic;
import com.design.util.moveBox.MapMethod;
import com.design.util.moveBox.MapParser;
import com.design.window.Route;
import com.design.util.moveBox.select.PanelMap;
import java.awt.*;
import java.io.File;
public class SelectMap extends Route {
    private PanelMap<MapMethod> panelMap;
    public SelectMap(String href) {
        super(href, 800, 800, "推箱子", "选择地图");
    }
    @Override
    public boolean before() {
        File[] files = new File(GameFile.MAP_DIR).listFiles();
        if(files == null){
            files = new File[0];
        }
        MapMethod[] methods = new MapMethod[files.length];
        for(int i = 0; i < files.length; i ++){
            try{
                methods[i] = MapParser.parser(files[i]);
            } catch (ParserError parserError) {
                parserError.printStackTrace();
            }
        }
        this.panelMap = new PanelMap<>(methods);
        for(int i = 0; i < methods.length; i ++){
            if(methods[i] != null){
                continue;
            }
            this.panelMap.setStatic(MapStaticEnum.ERROR,i);
        }
        return true;
    }

    @Override
    protected boolean close() {
        return true;
    }

    @Override
    protected void run() {

    }
    @Override
    protected void keyPressed(char key) {
        switch(key){
            case 'd':
                this.panelMap.add(true);
            break;
            case 'a':
                this.panelMap.add(false);
                break;
            //回车
            case 10 :
                MapStatic<MapMethod> mapStatic = this.panelMap.getMapStatic();
                if(mapStatic.getType() != MapStaticEnum.SUCCESS){
                    break;
                }
                MapMethod method = mapStatic.getFile();
                try{
                    new PlayMoveBox(method.getFile()).setVisible(true);
                } catch (ParserError parserError) {
                    parserError.printStackTrace();
                }
            break;
        }
        this.refill();
    }
    @Override
    protected void filling(Graphics g) {
        g.setColor(Color.WHITE);
        g.fillRect(0,0,this.getWindowWidth(),this.getWindowWidth());
        g.setColor(Color.RED);
        MapStatic<MapMethod>[] mapStatics = this.panelMap.getMapStatics();
        int margin = 40;
        int width = (this.getWindowWidth() - margin) / (MoveBoxConfig.PAGE_COLUMN + 1);
        for(int i = 0,key = 0; i < MoveBoxConfig.PAGE_SIZE; i ++){
            for(int j = 0; j < MoveBoxConfig.PAGE_COLUMN && key < mapStatics.length; j ++,key ++){
                int x = j * width + margin * j + margin;
                int y = i * width + margin * i + margin;
                MapStatic<MapMethod> mapStatic = mapStatics[key];
                switch(mapStatic.getType()){
                    case SUCCESS :
                        FillBox.fill(g,mapStatic.getFile(),width,x,y);
                        break;
                    case ERROR :
                        g.drawString("文件损坏",x + width / 2,y + width / 2);
                        break;
                    case DELETE :
                        g.drawString("文件已删除",x + width / 2,y + width / 2);
                        break;
                }
                if(key == this.panelMap.getKey()){
                    g.fillRect(x,y,width,30);
                }
            }
        }
    }
}
